package com.cn.kehong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IProductDianciRecordService;
import com.cn.kehong.api.IProductPackageService;
import com.cn.kehong.api.IProductReworkService;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.cn.kehong.utils.TemplateExcelUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productRework")
public class ProductReworkController {

  @Autowired
  private IProductReworkService iProductReworkService;

  @Autowired
  private IProductDianciRecordService productDianciRecordService;
  @Autowired
  private IProductPackageService iProductPackageService;
  @RequestMapping("/getProductReworkList/{frontToken}")
  @ResponseBody
  public JsonResponse<List<ProductRework>> getProductReworkList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
    JsonResponse<List<ProductRework>> response = new JsonResponse<>();
    List<ProductRework> productReworks = iProductReworkService.getProductReworkList(queryForm);
    response.setResult(productReworks);
    response.setSuccess(true);
    return response;
  }


  //app端 返工记录
  @RequestMapping("/getProductReworkByApp")
  @ResponseBody
  public JsonResponse<Map<String, Object>> getProductReworkByApp(@RequestBody QueryForm queryForm) throws Exception {
    JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
    Map<String, Object> productReworkByApp = iProductReworkService.getProductReworkByApp(queryForm);
    response.setResult(productReworkByApp);
    response.setSuccess(true);
    return response;
  }

  // 返工记录新增
  @RequestMapping("/addProductRework")
  @ResponseBody
  public JsonResponse<String> addProductRework(@RequestBody ProductRework productRework) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    Map<String, String> map = new HashMap<>();
    String result = iProductReworkService.addProductRework(productRework, map);
    if ("success".equals(result)) {
      response.setResult(map.get("id"));
      response.setMessage(map.get("message"));
      response.setSuccess(true);
    } else {
      response.setMessage(map.get("message"));
      response.setSuccess(false);
    }
    return response;
  }
  // 返工记录修改
  @RequestMapping("/updateProductRework")
  @ResponseBody
  public JsonResponse<String> updateProductRework(@RequestBody ProductRework productRework) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = iProductReworkService.updateProductRework(productRework);
    return CommonUtil.getResponseData(response, result);
  }

  // 根据电除磁罐号查询当前罐中进行中的制程批次
  @RequestMapping("/getDianProductCode")
  @ResponseBody
  public JsonResponse<List<ProductDianciRecord>> getDianProductCode(@RequestBody QueryForm queryForm) throws Exception {
    JsonResponse<List<ProductDianciRecord>> response = new JsonResponse<>();
    List<ProductDianciRecord> productDianciRecords = productDianciRecordService.getDianProductCode(queryForm);
    response.setResult(productDianciRecords);
    response.setSuccess(true);
    return response;
  }


  //返工管理下载
  @ResponseBody
  @RequestMapping("/exportProductRework")
  public void exportProductRework(String queryStr, HttpServletResponse response) throws Exception {
    QueryForm queryForm = new QueryForm();
    if(StringUtils.isNotEmpty(queryStr)){
      queryStr = URLDecoder.decode(queryStr,"utf-8");
      queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
      JSONObject jsonObject = JSON.parseObject(queryStr);
      queryForm.setStartTime(jsonObject.getString("startTime"));
      queryForm.setEndTime(jsonObject.getString("endTime"));
      queryForm.setFactoryName(jsonObject.getString("factoryName"));
      queryForm.setBanCi(jsonObject.getString("bancCi"));
      queryForm.setProductCode(jsonObject.getString("productCode"));
      queryForm.setPackageCode(jsonObject.getString("packageCode"));

    }
    iProductReworkService.exportProductRework(queryForm,response);
  }


  //通过生产批次查询成品批次返工记录
  @RequestMapping("/getProductByCode/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String,Object>> getProductByCode(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm ) throws Exception {
    JsonResponse<Map<String,Object> > response = new JsonResponse<Map<String,Object> >();
    Map<String,Object> result = iProductPackageService.getProductByCode(queryForm);
    response.setResult(result);
    String errorMsg = (String) result.get("errorMsg");
    response.setSuccess(StringUtils.isEmpty(errorMsg)?true:false);
    return  response;
  }
}
